package lt.marsrutai.mobile.android.intercept;

import java.lang.reflect.Method;
import java.util.Arrays;

import lt.marsrutai.mobile.android.cache.DataCache;
import lt.marsrutai.mobile.android.dao.annotation.Cache;

public class CacheInterceptor implements Interceptor {

    private DataCache dataCache;

    public CacheInterceptor(DataCache dataCache) {
        this.dataCache = dataCache;
    }

    @Override
    public Object interceptBefore(Method method, Object[] args)
    {
        if (null == method.getAnnotation(Cache.class)) {
            return null;
        }

        return dataCache.getCachedData(method.getName(), Arrays.asList(args));
    }

    @Override
    public void interceptAfter(Method method, Object[] args, Object result)
    {
        if (null == method.getAnnotation(Cache.class)) {
            return;
        }

        dataCache.cacheData(method.getName(), Arrays.asList(args), result);
    }

    public int getCacheSize() {
        return dataCache.getSize();
    }
}
